package com.example.petstore.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.LineItem;
import com.example.petstore.domain.Item;
import com.example.petstore.domain.Order;
import com.example.petstore.domain.User;
import com.example.petstore.service.LineItemService;
import com.example.petstore.service.ItemService;
import com.example.petstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@SessionAttributes({"loginUser"})
@SessionAttributes({"loginAdmin"})
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private LineItemService lineItemService;

//    @ResponseBody
//    @GetMapping("/admin/getNoShipOrder")/*test ok*/
//    public CommonResponse getNoShipOrder(Model model) {
//        List<Order> noShipOrderList = orderService.getOrdersByStatus3(true,false,true,true);
//        System.out.println(noShipOrderList);
//        return CommonResponse.creatForSuccess(noShipOrderList);
//    }
//    @ResponseBody
//    @GetMapping("/admin/getShipOrder")/*test ok*/
//    public CommonResponse getShipOrder() {
//        List<Order> ShipOrderList = orderService.getOrdersByStatus2(false,true,false,false);
//        System.out.println(ShipOrderList);
//        return CommonResponse.creatForSuccess(ShipOrderList);
//    }

    @ResponseBody
    @GetMapping("/admin/getOrder")/*test ok*///获取详细订单信息
    public CommonResponse getNoOrder(@RequestParam("orderId") String orderId, Model model) {
        List<LineItem> OrderList = lineItemService.getLineItemsByOrderId(Integer.parseInt(orderId));
        System.out.println(OrderList);
        model.addAttribute("detailOrderList",OrderList);
        return CommonResponse.creatForSuccess(OrderList);
//    }
    }

    @GetMapping("/admin/sendOrder")/*test ok*///发货
    public String sendOrder(@RequestParam("orderId") String orderId) {
        orderService.updateIssendByOrderId(Integer.parseInt(orderId));

        return "seller_order";
    }

    @ResponseBody
    @PostMapping("/admin/order_status")//订单筛选
    public CommonResponse<List<Order>> getSelectedOrder(@RequestBody String data,Model model) {

        System.out.println(data);

        int status1 = Character.getNumericValue(data.charAt(11));
        System.out.println(status1);
        int status2 = Character.getNumericValue(data.charAt(12));
        System.out.println(status2);
        int status3 = Character.getNumericValue(data.charAt(13));
        int status4 = Character.getNumericValue(data.charAt(14));
        int status5 = Character.getNumericValue(data.charAt(15));
        System.out.println(status3);
        System.out.println(status4);
        System.out.println(status5);
        List<Order> selectedOrder = orderService.getOrdersByStatus(status1,status2,status3,status4,status5).getData();
        model.addAttribute("selectedOrder",selectedOrder);
       // System.out.println("test");
        System.out.println(selectedOrder);
       // System.out.println("seee");
        return orderService.getOrdersByStatus(status1,status2,status3,status4,status5);
    }

    @GetMapping("/admin/getOrderListByKeyForJS")
    @ResponseBody
    public CommonResponse getOrderListByKeyForJS(String keyword){
        System.out.println(keyword);
        System.out.println(orderService.getOrderListByKey(keyword));
        return orderService.getOrderListByKey(keyword);
    }

    @ResponseBody
    @GetMapping("/admin/deleteOrder")
    public CommonResponse deleteOrder(String orderId) {
        return orderService.deleteOrderById(orderId);
    }

}
